package com.future.mayi.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.dialog.CommonDialog;
import com.future.mayi.model.ProtocolItemModel;
import com.future.mayi.model.ProtocolModel;
import com.future.mayi.response.ProtocolResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.utils.HtmlFormat;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class JimaiAgreementActivity extends BaseTitleActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_card)
    TextView tvCard;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_all_goods)
    LinearLayout llAllGoods;
    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.tv_order_id)
    TextView tvOrderId;
    @BindView(R.id.tv_enter_time)
    TextView tvEnterTime;
    @BindView(R.id.tv_enter)
    TextView tvEnter;

    private LayoutInflater mInflater;
    private String orderId;
    private ProtocolModel model;

    public static void startActivity(Context context, String orderId) {
        context.startActivity(new Intent(context, JimaiAgreementActivity.class).putExtra("orderId", orderId));
    }


    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("寄卖协议单");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_jimai_agreement);
    }

    @Override
    protected void initView() {
        mInflater = LayoutInflater.from(this);


    }

    @Override
    protected void initDates() {
        orderId = getIntent().getStringExtra("orderId");
        getData();
    }

    @Override
    protected void initListener() {

    }


    @OnClick(R.id.tv_enter)
    public void onViewClicked() {
        tvEnter.setEnabled(false);
        new CommonDialog.Builder(this)
                .setCancelable(false)
                .setMessage("确定同意本次寄卖协议单吗？")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        tvEnter.setEnabled(true);
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        enter();
                    }
                }).show();


    }

    private void enter() {
        new HttpRequest<DefaultResponse>()
                .with(this).setApiCode(ApiCst.checkProtocol)
                .addParam("token", App.getToken(mContext))
                .addParam("order_id", orderId)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        getData();
                        toast(response.message);
                        tvEnter.setVisibility(View.GONE);
                        tvEnter.setEnabled(true);
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                        tvEnter.setEnabled(true);
                    }
                }).start(new DefaultResponse());
    }


    private void getData() {
        new HttpRequest<ProtocolResponse>()
                .with(this).setApiCode(ApiCst.getProtocol)
                .addParam("token", App.getToken(mContext))
                .addParam("order_id", orderId)
                .setListener(new HttpRequest.OnNetworkListener<ProtocolResponse>() {
                    @Override
                    public void onSuccess(ProtocolResponse response) {
                        model = response.model;
                        setView();
                    }

                    @Override
                    public void onFail(String message, ProtocolResponse response) {
                        toast(message);
                        finish();
                    }
                }).start(new ProtocolResponse());


    }

    private void setView() {
        if (model == null) return;
        //0 - 未生成 1 - 已生成 10 - 已确认
        if (model.getProtocol_status() == 0) {
            toast("协议未生成");
            finish();
            return;
        }
        tvEnter.setEnabled(model.getProtocol_status() == 1);
        tvEnter.setVisibility(model.getProtocol_status() == 1 ? View.VISIBLE : View.GONE);
        setText(tvName, model.getReal_name());
        setText(tvCard, model.getIdentity_number());
        setText(tvPhone, model.getContact_number());
        webView.loadDataWithBaseURL(null, HtmlFormat.getNewContent(model.getDesc()), "text/html", "utf-8", null);
        setText(tvOrderId, "寄卖订单号：" + model.getOrder_no());
        tvEnterTime.setVisibility(TextUtils.isEmpty(model.getCheck_time()) ? View.GONE : View.VISIBLE);
        setText(tvEnterTime, "确认时间：" + model.getCheck_time());
        llAllGoods.removeAllViews();
        if (model.getItem() != null && model.getItem().size() > 0) {
            for (ProtocolItemModel itemModel : model.getItem()) {
                View view = mInflater.inflate(R.layout.layout_jimai_agreement_item, llAllGoods, false);
                TextView tvBrand = view.findViewById(R.id.tv_brand);
                TextView tvStyle = view.findViewById(R.id.tv_style);
                TextView tvAttachment = view.findViewById(R.id.tv_attachment);
                TextView tvPrice = view.findViewById(R.id.tv_price);
                TextView tvNote = view.findViewById(R.id.tv_note);
                ImageView ivImg = view.findViewById(R.id.iv_img);
                llAllGoods.addView(view);
                setText(tvBrand, itemModel.getBrand_name());
                setText(tvStyle, itemModel.getColor());
                setText(tvAttachment, itemModel.getAttachment());
                setText(tvPrice, itemModel.getPrice());
                setText(tvNote, itemModel.getRemark());
                if (!TextUtils.isEmpty(itemModel.getImg())) {
                    HCUtils.loadWebImg(this, ivImg, itemModel.getImg());
                    ivImg.setTag(itemModel.getImg());
                    ivImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, PhotoActivity.class);
                            ArrayList<String> list = new ArrayList<>();
                            list.add((String) v.getTag());
                            intent.putExtra("listUrl", list);
                            mContext.startActivity(intent);
                        }
                    });
                }
            }
        }


    }


}
